﻿using System;
using GeoAPI.Geometries;
using NetTopologySuite.Geometries;
using NetTopologySuite.Geometries.Utilities;

namespace NetTopologySuite.Precision
{
	public class PrecisionReducerCoordinateOperation : GeometryEditor.CoordinateOperation
	{
		public PrecisionReducerCoordinateOperation(IPrecisionModel targetPrecModel, bool removeCollapsed)
		{
			this._targetPrecModel = targetPrecModel;
			this._removeCollapsed = removeCollapsed;
		}

		public override Coordinate[] Edit(Coordinate[] coordinates, IGeometry geom)
		{
			if (coordinates.Length == 0)
			{
				return null;
			}
			Coordinate[] array = new Coordinate[coordinates.Length];
			for (int i = 0; i < coordinates.Length; i++)
			{
				Coordinate coordinate = new Coordinate(coordinates[i]);
				this._targetPrecModel.MakePrecise(coordinate);
				array[i] = coordinate;
			}
			Coordinate[] array2 = new CoordinateList(array, false).ToCoordinateArray();
			int num = 0;
			if (geom is ILineString)
			{
				num = 2;
			}
			if (geom is ILinearRing)
			{
				num = 4;
			}
			Coordinate[] result = array;
			if (this._removeCollapsed)
			{
				result = null;
			}
			if (array2.Length < num)
			{
				return result;
			}
			return array2;
		}

		private readonly IPrecisionModel _targetPrecModel;

		private readonly bool _removeCollapsed = true;
	}
}
